#region License

// Copyright (c) 2013, ClearCanvas Inc.
// All rights reserved.
// http://www.clearcanvas.ca
//
// This file is part of the ClearCanvas RIS/PACS open source project.
//
// The ClearCanvas RIS/PACS open source project is free software: you can
// redistribute it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// The ClearCanvas RIS/PACS open source project is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// the ClearCanvas RIS/PACS open source project.  If not, see
// <http://www.gnu.org/licenses/>.

#endregion

using System;
using System.ServiceModel;
using ClearCanvas.ImageServer.Common.Utilities;
using ClearCanvas.ImageServer.Web.Common.Utilities;

namespace ClearCanvas.ImageServer.Web.Common.WebControls.Validators
{
    /// <summary>
    /// Validate a Web UI input control containing a filesystem path.
    /// </summary>
    /// <remarks>
    /// This validator calls a web service to validate the result. The web service must accept one parameter named "path".
    /// </remarks>
    /// 
    /// <example>
    /// <para>
    /// The following block adds validation for the filesystem path using a web service ValidateFilesystemPath 
    /// located at /Services/ValidationServices.asmx (relative to the current web site). 
    /// The parameter(s) is generated by the javascript script function ValidationFilesystemPathParams(). 
    /// </para>
    /// <para>
    /// If the path is not valid, the error indicator control InvalidInputIndicator will show up on the screen.
    /// </para>
    /// <code>
    ///  
    ///  function ValidationFilesystemPathParams()
    ///  {
    ///     input = document.getElementById('" + PathTextBox.ClientID + @"');
    ///     params = new Array();
    ///     params.path = input.value;
    ///
    ///     return params;
    ///  }
    /// 
    /// </code>
    /// 
    /// <code>
    /// <uc1:InvalidInputIndicator ID="InvalidPathHint" runat="server" 
    ///     ImageUrl="~/images/icons/HelpSmall.png"
    ///     Visible="true"/>
    ///                                                        
    /// <clearcanvas:FilesystemPathValidator runat="server" ID="PathValidator" 
    ///         ControlToValidate="PathTextBox"
    ///         InputName="Filesystem Path" 
    ///         InvalidInputColor="#FAFFB5" 
    ///         InvalidInputIndicatorID="InvalidPathHint"
    ///         ServicePath="/Services/ValidationServices.asmx" 
    ///         ServiceOperation="ValidateFilesystemPath"
    ///         ParamsFunction="ValidationFilesystemPathParams" 
    ///         ErrorMessage="Invalid path"
    ///         Display="None" ValidationGroup="vg1"/> 
    /// </code>
    /// </example>
    public class FilesystemPathValidator : WebServiceValidator
    {
        #region Protected Methods

        protected override bool OnServerSideEvaluate()
        {
            String path = GetControlValidationValue(ControlToValidate);

            if (String.IsNullOrEmpty(path))
            {
                ErrorMessage = ValidationErrors.FilesystemPathCannotBeEmpty;
                return false;
            }

            try
            {
                FilesystemInfo fsInfo = ServerUtility.GetFilesystemInfo(path);

                var r = fsInfo != null && fsInfo.Exists;
				if (!r) ErrorMessage = String.Format(ValidationErrors.FilesystemPathInvalidOrUnreachable, path);
	            return r;
            }
            catch (EndpointNotFoundException e)
            {
                ErrorMessage = e.Message;
                return true;
            }
            catch (IndexOutOfRangeException)
            {
                ErrorMessage = String.Format(ValidationErrors.FilesystemPathInvalidOrUnreachable, path);
                return false;
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;
                return false;
            }
        }

        #endregion Protected Methods
    }
}